package com.sfh.controller.task;

import com.jfinal.plugin.activerecord.Db;
import com.sfh.base.Task;
import com.sfh.service.model.NoteModel;
import com.sfh.service.model.table.NoteTable;
import com.sfh.service.util.SQLHelp;

/***
 * 處理帖子阅读数量
 * @author DELL
 *
 */
public class TaskNoteRead extends Task {
	long noteId;

	public TaskNoteRead(long noteId) {
		this.noteId = noteId;
	}
	@Override
	public boolean run() {

		NoteModel model = NoteModel.NOTE.findById(noteId);
		if (model == null) {
			return false;
		}
		final long version = model.getVersion();

		SQLHelp sql = new SQLHelp(NoteTable.Table);
		sql.addWhereEqualTo(NoteTable.id, noteId);
		sql.addWhereEqualTo(NoteTable.version, version);

		sql.updateKeys(NoteTable.version, version + 1);
		sql.updateKeys(NoteTable.readiSize, model.getReadiSize() + 1);

		int index = Db.update(sql.buildUpdaete());

		return index > 0;
	}

}
